﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Windows.Forms;
using System.Reflection;
using Wangzeran.CodeMaker.GUI.Modal;

namespace Wangzeran.CodeMaker.GUI.Codes
{
    /// <summary>
    /// Xml文件操作
    /// </summary>
    public class XmlMgt
    {
        /// <summary>
        /// AppSetting——当前的数据库字符串
        /// </summary>
        public const string APP_CONNSTRING = "connString";

        /// <summary>
        /// 获取相应数据库的连接字符串
        /// </summary>
        /// <param name="type">数据库类型</param>
        /// <returns></returns>
        public string GetConnString(string type)
        {
            try
            {
                Configuration cfg = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                if (cfg.ConnectionStrings.ConnectionStrings[type] == null)
                {
                    return null;
                }
                return cfg.ConnectionStrings.ConnectionStrings[type].ConnectionString;
            }
            catch (Exception ex)
            {	
                throw ex;
            }
        }

        /// <summary>
        /// 设置数据库连接字符串
        /// </summary>
        /// <param name="type">数据库类型</param>
        /// <param name="connString">连接字符串</param>
        public void SetConnString(string type, string connString)
        {
            try
            {
                Configuration cfg = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                if (cfg.ConnectionStrings.ConnectionStrings[type] == null)
                {
                    ConnectionStringSettings element =
                        new ConnectionStringSettings(type, connString);
                    cfg.ConnectionStrings.ConnectionStrings.Add(element);
                }
                cfg.ConnectionStrings.ConnectionStrings[type].ConnectionString = connString;
                if (cfg.AppSettings.Settings[APP_CONNSTRING] == null)
                {
                    KeyValueConfigurationElement element =
                        new KeyValueConfigurationElement(APP_CONNSTRING, type);
                    cfg.AppSettings.Settings.Add(element);
                }
                cfg.AppSettings.Settings[APP_CONNSTRING].Value = type;
                cfg.Save();
            }
            catch (Exception ex)
            {	
                throw ex;
            }
        }


        /// <summary>
        /// 获取相应数据库的连接字符串
        /// </summary>
        /// <param name="type">数据库类型</param>
        /// <returns></returns>
        public DBModal GetUseDatabase()
        {
            try
            {
                string type = ConfigurationManager.AppSettings[APP_CONNSTRING];

                Configuration cfg = 
                    ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                string connString = cfg.ConnectionStrings.ConnectionStrings[type].ConnectionString;
                //string connString = ConfigurationManager.ConnectionStrings[type].ConnectionString;
                DBModal dbModal = new DBModal(type, connString);
                return dbModal;
            }
            catch (NullReferenceException)
            {
                throw new Exception("未设置当前使用的数据库，请先配置数据库连接");
            }
        }

        /// <summary>
        /// 添加新模板
        /// </summary>
        /// <param name="filePath"></param>
        public void AddNewTemplate(string filePath)
        {
            try
            {
                //Configuration cfg = 
                //    ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

                //ConfigurationSection section = cfg.GetSection("codeMaker/templates");
                throw new NullReferenceException();
            }
            catch
            {
                throw;
            }
        }
    }
}
